﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Excel = Microsoft.Office.Interop.Access;
using System.IO;

namespace _4DJackPot
{
    public partial class DbSetting : Dock
    {
        public DbSetting()
        {
            InitializeComponent();
        }

        private void bT_db_Click(object sender, EventArgs e)
        {
            try
            {
                FileDialog FileDialog = new FileDialog();
                FileDialog.SetFilter("ACCESS DATA BASE(*.MDB)|*.MDB|ACCESS DATA BASE(*.ACCDB)|*.ACCDB");
                FileDialog.SetTitle("Search Database Path...");
                if ((tB_db.Text ?? "") != "") FileDialog.OpenDialog((tB_db.Text ?? ""));
                else FileDialog.OpenDialog();

                string fileDirectory = FileDialog.GetFileDirectory() ?? "";
                if (fileDirectory != "")
                {
                    tB_db.Text = fileDirectory;
                    if ((tB_db.Text ?? "") != "")
                    {
                        tB_db.Text += "\\" + FileDialog.GetFileName() ?? "";
                    }
                }
            }
            catch (Exception e1)
            {
                FileLog FileLog = new FileLog();
                FileLog.WriteErrorLog(e1.StackTrace + "\n" + e1.Message);
            }
        }

        private void bT_db_new_Click(object sender, EventArgs e)
        {
            try
            {
                FileDialog FileDialog = new FileDialog();
                FileDialog.SetFilter("ACCESS DATA BASE(*.MDB)|*.MDB|ACCESS DATA BASE(*.ACCDB)|*.ACCDB");
                //FileDialog.SetTitle("데이터베이스 경로 찾기");
                if ((tB_db.Text ?? "") != "") FileDialog.SaveDialog((tB_db.Text ?? ""), "");
                else FileDialog.SaveDialog();

                string fileDirectory = FileDialog.GetFileDirectory() ?? "";
                if (fileDirectory != "")
                {
                    tB_db.Text = fileDirectory;
                    if ((tB_db.Text ?? "") != "")
                    {
                        tB_db.Text += "\\" + FileDialog.GetFileName() ?? "";
                        try
                        {
                            if (File.Exists(tB_db.Text ?? ""))
                            {
                                File.Delete(tB_db.Text ?? "");
                            }
                            Microsoft.Office.Interop.Access.Application app = new Microsoft.Office.Interop.Access.Application();
                            app.NewCurrentDatabase(tB_db.Text);
                            app.Quit(Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveAll);
                        }
                        catch (Exception e1)
                        {
                            MessageBox.Show(e1.Message, "Help", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            FileLog FileLog = new FileLog();
                            FileLog.WriteErrorLog(e1.StackTrace + "\n" + e1.Message);
                        }
                    }
                }
            }
            catch (Exception e1)
            {
                FileLog FileLog = new FileLog();
                FileLog.WriteErrorLog(e1.StackTrace + "\n" + e1.Message);
            }
        }

        private void bTn_confirm_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                if (!ConnectTest()) return;

                Ini Ini = new Ini();

                // DATABASE 정의
                // MS ACCESS
             
                Ini.DefaultFileWriteIni("MACRO", "KINDDB", "0");
                Global.dbType = 0;

                // MS ACCESS
                // 위치
                Ini.DefaultFileWriteIni("MACRO", "DBPATH", this.tB_db.Text.Trim());
                Global.dbFileString = this.tB_db.Text.Trim();
               

                DbConn DbConn = new DbConn();
                if (DbConn.DbConnect(true))
                {
                    this.Dispose();
                }
            }
            catch (Exception e1)
            {
                FileLog FileLog = new FileLog();
                FileLog.WriteErrorLog(e1.StackTrace + "\n" + e1.Message);
            }
        }

        private bool ConnectTest()
        {
            try
            {
                if (!SaveValidCheck()) return false;

                DbConn DbConn = new DbConn();
                // MS ACCESS
                return DbConn.DbConnectTestMSAccess(this.tB_db.Text.Trim());
               
            }
            catch (Exception e1)
            {
                FileLog FileLog = new FileLog();
                FileLog.WriteErrorLog(e1.StackTrace + "\n" + e1.Message);
            }

            return true;
        }

        private bool SaveValidCheck()
        {
            try
            {
               
                    if (this.tB_db.Text.Trim() == "")
                    {
                        MessageBox.Show("MS ACCESS DB file name is not entered.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        this.tB_db.Focus();
                        return false;
                    }
                    else
                    {
                        if (!File.Exists(this.tB_db.Text.Trim()))
                        {
                            MessageBox.Show("MS ACCESS DB file does not exist.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            this.tB_db.Focus();
                            return false;
                        }
                    }
                
            }
            catch (Exception e1)
            {
                FileLog FileLog = new FileLog();
                FileLog.WriteErrorLog(e1.StackTrace + "\n" + e1.Message);
            }

            return true;
        }

        private void DbSetting_Load(object sender, EventArgs e)
        {
            try
            {
                Ini Ini = new Ini();
                // MS ACCESS
                // 위치
                this.tB_db.Text = Ini.DefaultFileReadIni("MACRO", "DBPATH");
                
            }
            catch (Exception e1)
            {
                FileLog FileLog = new FileLog();
                FileLog.WriteErrorLog(e1.StackTrace + "\n" + e1.Message);
            }
        }

        private void bTn_close_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
        }

    }
}
